package com.schuxl.htmlobserver.mail;

import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;

import com.schuxl.htmlobserver.StartHtmlObserver;

public class MailSender {

	private static final Logger LOGGER = Logger.getLogger(MailSender.class);
	private static final Properties SMTP_PROPERTIES = new Properties();
	private static String MAIL_FROM = null;

	static {
		SMTP_PROPERTIES.put("mail.smtp.host", StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.mail.smtp.host"));
		SMTP_PROPERTIES.put("mail.smtp.socketFactory.port", StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.mail.smtp.socketFactory.port"));
		SMTP_PROPERTIES.put("mail.smtp.socketFactory.class", StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.mail.smtp.socketFactory.class"));
		SMTP_PROPERTIES.put("mail.smtp.auth", StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.mail.smtp.auth"));
		SMTP_PROPERTIES.put("mail.smtp.port", StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.mail.smtp.port"));
		
		MAIL_FROM = StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.mail.from");
	}

	public static void sendMessage(Address[] empfaengerAdressen, String subject, String text) throws Exception {
		Session session = getMailSenderSession();
		LOGGER.debug("Erstelle MimeMessage");
		Message message = new MimeMessage(session);
		Multipart multiPart = new MimeMultipart();
		MimeBodyPart mimeBodyPart = new MimeBodyPart();
		mimeBodyPart.setContent(text, "text/html");
		multiPart.addBodyPart(mimeBodyPart);
		message.setContent(multiPart);
		message.setFrom(new InternetAddress(MAIL_FROM));
		message.setRecipients(Message.RecipientType.TO,	empfaengerAdressen);
		message.setSubject(subject);

		LOGGER.debug("Versende Message mittels SMTP.");
		Transport.send(message);
	}

	
	
	/**
	 * Erstellt die Session zum Versenden der Mails
	 * Entweder SSL-Session oder Non-SSL-Session - abhaengig vom System Property "com.schuxl.htmlobserver.mail.smtp.auth" 
	 * @return Session
	 */
	private static Session getMailSenderSession() {
		Session session = null;
		if("true".equals(StartHtmlObserver.PROPERTIES.getProperty("com.schuxl.htmlobserver.mail.smtp.auth"))) {
			LOGGER.debug("Erstelle SSL - Session fuer den Mail-Versand.");
			session = Session.getDefaultInstance(SMTP_PROPERTIES,
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication("brimizuza@gmail.com",
									"rantanplan6");
						}
					});
		} else {
			LOGGER.debug("Erstelle NonSSL - Session fuer den Mail-Versand.");
			session = Session.getDefaultInstance(SMTP_PROPERTIES);
		}
		return session;
	}
}
